package com.nuist.config;

import com.nuist.mapper.ElectricMapper;
import com.nuist.mapper.MeterMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/11/5 11:32
 */
@Slf4j
@Configuration
@DependsOn("flyway")
public class ApplicationConfiguration implements ApplicationRunner {
    @Resource
    private ElectricMapper electricMapper;
    @Resource
    private MeterMapper meterMapper;

    /**
     * 该方法会在所有单例bean实例化并且注入到ioc容器后执行
     *
     * @param args
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = {"Electric","Meter"},allEntries = true)
    public void run(ApplicationArguments args) throws Exception {
        log.info("开始清空electric表与meter表中的数据,同时清空redis缓存");
        electricMapper.delete(null);
        meterMapper.delete(null);
    }
}
